package com.rich.dmr.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.rich.dmr.R;
import com.rich.dmr.adapter.GroupListGridAdapter;
import com.rich.dmr.ui.view.SimpleToolbar;
import com.rich.dmr.utils.SoundPoolUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import rich.dmr.DmrManager;


public class GroupListActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = "rich---GroupListActivity";
    private DmrManager dmrManager;
    private List<String> groupList = new ArrayList<>();

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);
        dmrManager = (DmrManager) getSystemService("dmrservice");

        SimpleToolbar simpleToolbar = findViewById(R.id.main_tools_bar);
        simpleToolbar.setLeftTitleClickListener(this);
        simpleToolbar.setMainTitle(getString(R.string.tools_group));

        final RecyclerView recyclerView = findViewById(R.id.group_list);
        recyclerView.setLayoutManager(new MyStaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        String groupString = dmrManager.getGroupList();
        if (!groupString.equals("")) {
            groupList = Arrays.asList(groupString.split("/"));
        }

        final GroupListGridAdapter adapter = new GroupListGridAdapter(GroupListActivity.this, groupList);
        recyclerView.setAdapter(adapter);
        adapter.setOnClickListener(new GroupListGridAdapter.OnItemClickListener() {
            @Override
            public void ItemClickListener(View view, int position) {
                dmrManager.setTransmitContact(Integer.valueOf(groupList.get(position)));
                dmrManager.setContactType(1);
                dmrManager.flush();
                SoundPoolUtils.getInstance(GroupListActivity.this).playSound(false);
                finish();
            }

            @Override
            public void ItemLongClickListener(View view, final int position) {
                tipClick(GroupListActivity.this,position);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_left_title:
                finish();
                break;
            default:
                break;
        }
    }
    public void tipClick(final Context context, final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("delete group");
        builder.setMessage("\n   is delete this group?");
        builder.setIcon(R.mipmap.setting_about_icon);
        builder.setCancelable(true);
        builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int group = Integer.valueOf(groupList.get(position));
                Log.d(TAG, "group=" + group);
                dmrManager.removeGroup(group);
                dmrManager.flush();
                SoundPoolUtils.getInstance(GroupListActivity.this).playSound(false);
                dialog.dismiss();
                finish();
            }
        });
        //设置反面按钮
        builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }


    class MyStaggeredGridLayoutManager extends StaggeredGridLayoutManager {


        public MyStaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        public MyStaggeredGridLayoutManager(int spanCount, int orientation) {
            super(spanCount, orientation);
        }

        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                //这里捕获之前的数组越界问题...
                super.onLayoutChildren(recycler, state);
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }

        }
    }

}
